﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace RedSand.Site
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            // index page
            routes.MapRoute(
               "Index by page",
               "page/{page}",
               new { controller = "home", action = "index", page = UrlParameter.Optional },
               new { page = @"\d|PageIndex" }
            );

            routes.MapRoute(
               "Router for home",
               "{action}",
               new { controller = "home", action = "index" },
                new { controller = "home", action = @"mylist|topview|contact|rules" }
            );

            // RsController
            routes.MapRoute(
               "Preview Image",
               "Base/{action}/{id}",
               new { controller = "base", action = "index", id = UrlParameter.Optional }
            );  
            routes.MapRoute(
               "admin",
               "{controller}/{action}/{id}",
               new { controller = "home", action = "index", id = UrlParameter.Optional },
               new { controller = @"^(admin)|(category)|(action)|(account)|(user)|(role)$" }
            );
            routes.MapRoute(
               "rs ruoter",
               "RedSand/{action}/{id}",
               new { controller = "RedSand", action = "index", id = UrlParameter.Optional }
            );

            //create comment - frontend
            routes.MapRoute(
               "comment News",
               "reply/comment/{id}",
               new { controller = "reply", action = "feWriteComment", id = UrlParameter.Optional }
            );
            routes.MapRoute(
               "comment delete",
               "reply/delete/{newsId}/{id}",
               new { controller = "reply", action = "feDelete", newsId=UrlParameter.Optional, id = UrlParameter.Optional }
            );               
            // list comment - frontend
            routes.MapRoute(
               "list comment",
               "reply/list/{NewsID}/{page}",
               new { controller = "reply", action = "feComment", NewsID = UrlParameter.Optional, page = UrlParameter.Optional }
            );
            // Reply router
            routes.MapRoute(
               "Get Comment by page",
               "reply/{action}/{NewsID}/{page}",
               new { controller = "reply", action = "index", NewsID = UrlParameter.Optional, page = 1 },
               new { page = @"\d|PageIndex" }
            );

            // Create news - frontend
            routes.MapRoute(
               "Create News",
               "create",
               new { controller = "post", action = "feCreate" }
            );              
            // Edit news - frontend
            routes.MapRoute(
               "Edit News",
               "edit/{AliasNews}",
               new { controller = "post", action = "feEdit", AliasNews = UrlParameter.Optional },
               new { AliasNews = @"(.+)(.html)$" }
            );

            // Edit news - frontend
            routes.MapRoute(
               "Delete News",
               "delete/{AliasNews}",
               new { controller = "post", action = "feDetele", AliasNews = UrlParameter.Optional },
               new { AliasNews = @"(.+)(.html)$" }
            );

            // account - frontend
            routes.MapRoute(
               "account register",
               "register",
               new { controller = "account", action = "feRegister" }
            );
             routes.MapRoute(
               "account changepassword",
               "changepassword",
               new { controller = "account", action = "feChangePassword" }
            );
             routes.MapRoute(
               "account forgotpassword",
               "forgotpassword",
               new { controller = "account", action = "feForgotPassword" }
            );
             routes.MapRoute(
               "account myprofile",
               "myprofile",
               new { controller = "account", action = "feEditProfile" }
            );


            // recover - frontend
            routes.MapRoute(
               "recover",
               "recover/{username}/{password}/{datetime}",
               new { controller = "account", action = "feResetPassword", username = UrlParameter.Optional, password = UrlParameter.Optional, datetime = UrlParameter.Optional }
            );

            // search - frontend
            routes.MapRoute(
               "search",
               "search/{keyword}",
               new { controller = "home", action = "index", keyword = UrlParameter.Optional }
            );                                  


            // news routes
            routes.MapRoute(
               "List by AliasParent, Page",
               "{AliasParent}/page/{page}",
               new { controller = "post", action = "feListByCategory", AliasParent = UrlParameter.Optional, page = 1 },
               new { page = @"\d|PageIndex", AliasParent = @".+" }
            );

            routes.MapRoute(
               "Details News - Parent Category",
               "{AliasParent}/{AliasNews}/{page}",
               new { controller = "post", action = "feDetails", AliasParent = UrlParameter.Optional, AliasNews = UrlParameter.Optional, page = 1 },
               new { AliasParent = @".+", AliasNews = @"(.+)(.html)$", page = @"\d|PageIndex" }
            );

            routes.MapRoute(
               "List by AliasParent",
               "{AliasParent}/{page}",
               new { controller = "post", action = "feListByCategory", AliasParent = UrlParameter.Optional, page = 1 },
               new { AliasParent = @".+", page = @"\d|PageIndex" }
            );

            routes.MapRoute(
               "List by AliasParent, AliasChild, Page - part /page/",
               "{AliasParent}/{AliasChild}/page/{page}",
               new { controller = "post", action = "feListByCategory", AliasParent = UrlParameter.Optional, AliasChild = UrlParameter.Optional, page = 1 },
               new { page = @"\d|PageIndex", AliasParent = @".+", AliasChild = @".+" }
            );

            routes.MapRoute(
               "Details News - Child Category",
               "{AliasParent}/{AliasChild}/{AliasNews}/{page}",
               new { controller = "post", action = "feDetails", AliasParent = UrlParameter.Optional, AliasChild = UrlParameter.Optional, page = 1 },
               new { AliasParent = @".+", AliasChild = @".+", AliasNews = @"(.+)(.html)$", page = @"\d|PageIndex" }
            );

            routes.MapRoute(
               "List by AliasParent,AliasChild",
               "{AliasParent}/{AliasChild}/{page}",
               new { controller = "post", action = "feListByCategory", AliasParent = UrlParameter.Optional, AliasChild = UrlParameter.Optional, page = 1 },
               new { AliasParent = @".+", AliasChild = @".+", page = @"\d|PageIndex" }
            );



            // default router 
            routes.MapRoute(
              "Index ",
              "{controller}/{action}",
              new { controller = "home", action = "index" }
            );

        }

        protected void Application_Start()
        {
            if (!RedSand.Web.Mvc.RsDataManager.TestConnection())
            {
                throw new Exception("Lỗi kết nối cơ sở dữ liệu");
            }

            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }
    }
}